﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using CrclzChat.Areas.Identity.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace CrclzChat.Api.Register
{
	[Route("api/Register/[controller]")]
	[ApiController]
	public class SubmitController : ControllerBase
	{
		private UserManager<OneUser> _userManager { get; set; }

		public SubmitController(UserManager<OneUser> userManager)
		{
			_userManager = userManager;
		}

		[HttpPost]
		public async Task<IActionResult> Post([FromBody]RegisterInputModel info)
		{
			if (ModelState.IsValid == false)
			{
				return new BadRequestObjectResult(ModelState);
			}

			var regResult = await _userManager.CreateAsync(new OneUser
			{
				UserName = info.Username
			}, info.Password);

			return new JsonResult(new { ok = regResult.Succeeded, msg = regResult.ToString() });
		}
	}

	public class RegisterInputModel
	{
		[MaxLength(16)]
		[MinLength(5)]
		[RegularExpression("[a-z]+[a-z0-9]+")]
		public string Username { get; set; }

		[MaxLength(32)]
		[MinLength(8)]
		[RegularExpression("[a-zA-Z0-9_.\\-+!@#$%^&*]+")]
		public string Password { get; set; }

		[Compare("Password")]
		public string Password2 { get; set; }
	}
}